package com.zhongxi.guoyuan.view.seller;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.seller.CenterLoginResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

public class SellerRewardLoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText etPhone;
    private EditText etPassword;
    private TextView tvLogin;
    private TextView tvForgot;
    private TextView tvTip;
    private Map<String, String> map = new HashMap<String, String>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_seller_reward_login);
        initTitle();
        tvTitle.setText("线上赚豆超市登录");
        etPhone = (EditText) findViewById(R.id.etPhone);
        etPassword = (EditText) findViewById(R.id.etPassword);
        tvLogin = (TextView) findViewById(R.id.tvLogin);
        tvForgot = (TextView) findViewById(R.id.tvForgot);
        tvTip = (TextView) findViewById(R.id.tvTip);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        tvForgot.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvLogin:
                String phone = etPhone.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    showShortToast("手机号不能为空");
                } else if (TextUtils.isEmpty(password)) {
                    showShortToast("密码不能为空");
                } else {
                    login(phone, password);
                }
                break;
            case R.id.tvForgot:
                tvTip.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 商家中心登录
     *
     * @param phone
     * @param password
     */
    private void login(String phone, String password) {
        map.clear();
        map.put("mphone", MyApplication.getInstance().descsc(phone));
        map.put("pwd", MyApplication.getInstance().descsc(password));
        map.put("type", "2");
        getServer(Url.CENTER_LOGIN, map, "", false, null, "post");
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            CenterLoginResult result = gson.fromJson(response, CenterLoginResult.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj()))
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            MyApplication.getInstance().sharedPrefrences.edit().putString("rewardcoincredentials", result.getData().getCredentials()).commit();
//            showShortToast("登录成功");
            startAct(SellerRewardCoinActivity.class);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        CenterLoginResult result = gson.fromJson(response, CenterLoginResult.class);
        if ("9001".equals(result.getCode())) {
            showShortToast("用户已登录");
            startAct(SellerRewardCoinActivity.class);
        } else {
            showShortToast("账号或密码错误");
        }
    }
}
